<template>
  <div class="container">
    <div class="left">
      <div class="left-top">
        <div class="logo"></div>
        <span class="logo-content">时光校园</span>
      </div>
      <div class="left-bottom">
        <div class="bottom__content">
          <div class="first-line">“每一件失物，都是未完待续的故事，</div>
          <div class="second-line">让丢失成为重逢的开始。”</div>
        </div>
        <div class="other-msg">
          <div class="item">
            <div class="iconfont icon-right-1"></div><span>极速匹配</span>
          </div>
          <div class="item">
            <div class="iconfont icon-right-1"></div><span>精准寻物</span>
          </div>
          <div class="item">
            <div class="iconfont icon-right-1"></div><span>零基础操作</span>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="form">
        <div style="
            text-align: center;
            font-size: 22px;
            margin-bottom: 20px;
            color: #333;
          ">
          注册你的时光账户
        </div>
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" placeholder="请确认密码" show-password
              v-model="form.confirmPass"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="
                width: 100%;
                background-color: #2a82e4;
                border-color: #2a82e4;
                color: white;
              " @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex; align-items: center">
            <div style="flex: 1; text-align: right">
              已有账号？请 <router-link to="/login">登录</router-link>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    // 验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === "") {
        callback(new Error("请确认密码"));
      } else if (confirmPass !== this.form.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      form: { role: "USER" },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" },
        { validator: this.vaildateUsername, trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }, { validator: this.validatePasswordFormat, trigger: 'blur' }],
        confirmPass: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  created() { },
  methods: {
    validatePasswordFormat(rule, value, callback) {
      if (/^\w{6,10}$/.test(value)) {
        callback()
      } else {
        callback(new Error('密码长度为6-10个字符'))
      }

    },
    vaildateUsername(rule, value, callback) {
      if (/^\w{6,12}$/.test(value)) {
        callback()
      } else {
        callback(new Error('用户名长度为6-12个字符'))
      }

    },
    register() {
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post("/register", this.form).then((res) => {
            if (res.code === "200") {
              this.$router.push("/login"); // 跳转登录页面
              this.$message.success("注册成功");
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/background.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;

  .left {
    flex: 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;

    .left-top {
      display: inline-flex;
      width: 100%;
      padding: 50px;
      align-items: center;

      .logo {
        background: url("@/assets/imgs/logo.png") center -11px/150% no-repeat;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        overflow: hidden;
      }

      .logo-content {
        font-size: 22px;
        color: #fff;
        font-family: "Kingsoft_Cloud_Font";
        margin-left: 12px;
      }
    }

    .left-bottom {
      margin-top: 10%;

      .bottom__content {
        color: #000;
        font-size: 24px;
        font-weight: 700;
        font-family: "Kingsoft_Cloud_Font";

        .second-line {
          margin-left: 12%;
        }
      }

      .other-msg {
        margin-top: 30px;
        margin-left: 30px;
        display: flex;
        gap: 8px;

        .item {
          display: inline-flex;
          align-items: center;
          gap: 3px;
        }

        .iconfont {
          color: #fff;
        }
      }
    }
  }

  .right {
    flex: 2;

    .form {
      margin-left: 80px;
      width: 340px;
      padding: 40px 40px;
      background-color: white;
      border-radius: 5px;
    }
  }
}

a {
  color: #2a60c9;
}
</style>
